// 导入响应拦截器
import request from '@/utils/request.js'
import axios from 'axios'

// 我的高德key
const apiKey = '760dcc9943d2bd9b589689c29dfc8b9c'

// 获取ip所在城市
export const getCityApi = () => {
  return request({
    url: '/ip',
    params: { key: apiKey },
  })
}
// 通过城市名查询adcode
export const getAdcodeApi = cityName => {
  return request({
    url: '/geocode/geo',
    params: {
      key: apiKey,
      address: cityName,
    },
  })
}
// 通过adcode查询城市天气预报
export const getWeatherForecastApi = adcode => {
  return request({
    url: '/weather/weatherInfo',
    params: {
      key: apiKey,
      city: adcode,
      extensions: 'all',
    },
  })
}
// 通过adcode查询城市实况天气
export const getWeatherNowApi = adcode => {
  return request({
    url: '/weather/weatherInfo',
    params: {
      key: apiKey,
      city: adcode,
      extensions: 'base',
    },
  })
}
// 获取城市信息
export const getCityInfo = async cityName => {
  try {
    const response = await axios.get(
      `http://hmajax.itheima.net/api/weather/city?city=${cityName}`
    )
    return response.data.data
  } catch (error) {
    console.error('Error fetching city info:', error)
    return null
  }
}
